<?php
/**
 * PermissionController
 * 
 * @author
 * @version 
 */
class Admin_PermissionController extends Sunnet_Controller_Action
{
	var $_arrParam;
	public function init(){
		Zend_Loader::loadClass('Admin_Models_RoleModel');
		Zend_Loader::loadClass('Admin_Models_ActionModel');
		Zend_Loader::loadClass('Admin_Models_ControllerModel');
		Zend_Loader::loadClass('Admin_Models_UserAdminModel');
		Zend_Loader::loadClass('Admin_Models_PermissionManagerModel');
		$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
        
             Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu; 
	}
	
	public function preDispatch(){
	    $auth = Zend_Auth::getInstance();
    	if (!$auth->hasIdentity()  || !$auth->getIdentity()->admin) {
    		$this->_forward('login','index','admin',array('action'	=> 'login'));//,$this->_arrParam);
    	}
		$template_path = TEMPLATE_PATH . "/admin/default";
		$this->loadTemplate($template_path,'template.ini','template');
		$this->view->controller = $this->_request->getControllerName();
		$this->view->module = $this->_request->getModuleName();
		$this->view->headScript()->appendFile('/js/permission.js','text/javascript');
	}
    /**
     * The default action - show the home page
     * name Phân quyền
     */
    public function indexAction ()
    {
        // TODO Auto-generated PermissionController::indexAction() default action
        $request = $this->_request;
        $user_id = $request->getParam('user_id');
        if (!$user_id){
        	$user_id = 1;
        }
        $roleModel = new Admin_Models_RoleModel();
        if ($request->isPost()){
        	$role_list = $request->getParam('role_list');
        	$where = $roleModel->getAdapter()->quoteInto('user_id = ?', $user_id);
        	$roleModel->delete($where);
        	foreach ($role_list as $role){
        		$data = array(	'user_id'	=> $user_id,
        						'action_id'	=>$role);
        		$roleModel->insert($data);
        	}
        }
        $model_user = new Admin_Models_UserAdminModel();
        $model_action = new Admin_Models_ActionModel();
        $model_role = new Admin_Models_RoleModel();
        $user_list = $model_user->getAllUserList();
        $action_list = $model_action->getActionList();
        $role_list = $model_role->getUserPermisstion($user_id);
        $this->view->action_list = $action_list;
        $this->view->role_list = $role_list;
        $this->view->user_id = $user_id;
        $this->view->user_list = $user_list;
    }
    /**
     * Quản lý danh sách admin
     */
    public function addPermisstionManagerAction(){
        $user_list = Admin_Models_UserAdminModel::getAllUserList();
        if ($this->_request->isPost()){
        	$user_id = $this->_request->getParam('user_id');
        	$mode = $this->_request->getParam('mode');
        	$permissionManagerModel = new Admin_Models_PermissionManagerModel();
        	switch ($mode) {
        		case 'add':
        			if ($user_id){
		        		$data = array('user_id' => $user_id);
		        		$permissionManagerModel->insert($data);
        			}
	        		break;
        		case 'delete':
        			$where = $permissionManagerModel->getAdapter()->quoteInto('user_id = ?', $user_id);
        			$permissionManagerModel->delete($where);
        			break;
        	}
        }
    	$permission_manager_list = Admin_Models_PermissionManagerModel::getPermissionManagerList();
    	$this->view->user_list = $user_list;
    	$this->view->permission_manager_list = $permission_manager_list;
    }
    /**
     * Quản lý danh mục
     */
    public function addControllerAction(){
  		if ($this->_request->isPost()){
        	$mode = $this->_request->getParam('mode');
        	$controllerModel = new Admin_Models_ControllerModel();
        	switch ($mode) {
        		case 'add':
        			$data = array(	'controller'	=> $this->_request->getParam('controller_txt'),
        							'name'			=> $this->_request->getParam('name'));
        			$controllerModel->insert($data);
	        		break;
        		case 'delete':
        			$id = $this->_request->getParam('id');
        			$where = $controllerModel->getAdapter()->quoteInto('id = ?', $id);
        			$controllerModel->delete($where);
        			break;
        		case 'edit':
        			$id = $this->_request->getParam('id');
        			$where = $controllerModel->getAdapter()->quoteInto('id = ?', $id);
        			$data = array(	'controller'	=> $this->_request->getParam('controller_txt'),
        							'name'			=> $this->_request->getParam('name'));
        			$controllerModel->update($data, $where);
        			break;
        	}
        }
    	$controller_list = Admin_Models_ControllerModel::getControllerList();
    	$this->view->controller_list = $controller_list;
    }
    /**
     * Quản lý quyền
     */
	public function addActionAction(){
  		if ($this->_request->isPost()){
        	$mode = $this->_request->getParam('mode');
        	$actionModel = new Admin_Models_ActionModel();
        	switch ($mode) {
        		case 'add':
        			$data = array(	'action'		=> $this->_request->getParam('action_txt'),
        							'name'			=> $this->_request->getParam('name'),
        							'controller_id'	=> $this->_request->getParam('controller_id'));
        			$actionModel->insert($data);
	        		break;
        		case 'delete':
        			$id = $this->_request->getParam('id');
        			$where = $actionModel->getAdapter()->quoteInto('id = ?', $id);
        			$actionModel->delete($where);
        			break;
        		case 'edit':
        			$id = $this->_request->getParam('id');
        			$where = $actionModel->getAdapter()->quoteInto('id = ?', $id);
        			$data = array(	'action'		=> $this->_request->getParam('action_txt'),
        							'name'			=> $this->_request->getParam('name'),
        							'controller_id'	=> $this->_request->getParam('controller_id'));
        			$actionModel->update($data, $where);
        			break;
        	}
        }
        $model_controller = new Admin_Models_ControllerModel();
    	$controller_list = $model_controller->getControllerList();
        $model_action = new Admin_Models_ActionModel();
    	$action_list = $model_action->getActionList();
    	$this->view->controller_list = $controller_list;
    	$this->view->action_list = $action_list;
    }
}
